package com.tpl.core.basedata.common.task;

import com.tpl.core.basedata.dao.TriggerTaskDao;
import com.tpl.core.common.Global;
import com.tpl.core.dao.ExecuteSql;
import com.tpl.core.entity.sys.TriggerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;

public class TaskUtil {
	private static Log log= LogFactory.getLog(TaskUtil.class);
	private static Scheduler scheduler = (Scheduler) Global.getContext().getBean("scheduler");
	private static TriggerTaskDao triggerTaskDao=(TriggerTaskDao) Global.getContext().getBean("triggerTaskDao");
	/**
	 * 启动定时任务
	 */
	public static void startTask(Long taskId){
		TriggerTask task=triggerTaskDao.getTaskById(taskId);
		if(task==null)
			log.error("not found trgger task by id"+taskId);
		else
			startTask(task);
	}
	/**
	 * 启动定时任务
	 * @param task
	 */
	public static void startTask(TriggerTask task){
		try {
			ExecuteSql.execute("update BD_TRIGGER_TASK set state=1 where id=?", task.getId());
			String triggerName = "trigger"+task.getId();
			String jobName = "job"+task.getId();
//			String listenerName = "listener"+task.getId();
			MethodInvokingJobDetailFactoryBean factory = new MethodInvokingJobDetailFactoryBean();
			factory.setTargetObject(new TriggerTaskExecute());
			factory.setTargetMethod("execute");
			factory.setName(jobName);
			factory.setArguments(new Object[]{task.getId(),task.getSourceId(),task.getSourceServiceName()});
//			factory.setGroup(IConstants.DEFAULT_GROUP_NAME);
			factory.afterPropertiesSet();
			JobDetail job = (JobDetail)factory.getObject();
//			CronTrigger trigger = new CronTrigger(triggerName,IConstants.DEFAULT_GROUP_NAME,task.getCronExpression());
//			TaskFinishTriggerListener finishTriggerListener = new TaskFinishTriggerListener(listenerName);
//			trigger.setStartTime(new Date());
//			trigger.addTriggerListener(listenerName);
//			scheduler.addTriggerListener(finishTriggerListener);
//			scheduler.scheduleJob(job, trigger);
			log.info("Job loaded:"+task.getName());
		} catch (Exception e) {
			log.error("Trigger Jobs start failed:"+e.getCause());
		}
	}
	/**
	 * 获取定时任务
	 * @param sourceServiceName
	 * @param sourceId
	 * @return
	 */
	public static TriggerTask getTask(String sourceServiceName,String sourceId){
		TriggerTask task=triggerTaskDao.getTask("sourceServiceName="+sourceServiceName,"sourceId="+sourceId);
		return task;
	}
	/**
	 * 创建定时任务
	 * @param task
	 * @return
	 */
	public static TriggerTask createTask(TriggerTask task){
		task.setCreateDate(task.getUpdateDate());
		task.setCreator(task.getUpdator());
		triggerTaskDao.insert(task);
		return task;
	}
	/**
	 * 停止定时任务
	 * @param id
	 */
	public static void stopTask(Long id) {
		try {
			ExecuteSql.execute("update BD_TRIGGER_TASK set state=0 where id=?", id);
			String triggerName = "trigger"+id;
			String jobName = "job"+id;
			String listenerName = "listener"+id;
//			scheduler.unscheduleJob(triggerName, IConstants.DEFAULT_GROUP_NAME);
//			scheduler.deleteJob(jobName, IConstants.DEFAULT_GROUP_NAME);
//			scheduler.removeTriggerListener(listenerName);
		} catch (Exception e) {
			log.error("Trigger Jobs stop failed:"+e.getCause());
		}
	}
	
}
